/*
 * @Author: ly
 * @Date: 2019-10-01 16:31:28
 * @Last Modified by: ly
 * @Last Modified time: 2019-11-07 10:56:51
 */

export function trimString(str: string): string {
  return str;
}

export function checkReg(key: string, value: string): boolean {
  const regs: any = {
    name: /^[\w\u4e00-\u9fa5]{2,20}$/,
    mobile: /^1[35-9][0-9]{9}$/,
    address: /^[-,.#\w\u4e00-\u9fa5]{2,80}$/,
  };
  if (key && value && regs[key] && regs[key].test(value)) {
    return true;
  }
  return false;
}

export function getMoveWidth(viewportHeight: number): number {
  return Math.tan((18 * Math.PI) / 180) * viewportHeight;
}

export function getSeconds(countdown: number, duration: number) {
  let seconds = 0;
  if (countdown > 0) {
    seconds += countdown;
  }
  if (duration > 0) {
    seconds += duration;
  }
  return seconds;
}

interface IRedpacketRainConfig {
  willStart: boolean;
  willStop: boolean;
  frameRate: number;
  viewportWidth: number;
  viewportHeight: number;
  moveWidth: number;
  redpacketHeight: number;
  redpacketWidth: number;
  redpacketFate: number;
  starFate: number;
  starHeight: number;
  starWidth: number;
  durationMin: number;
  durationMax: number;
  delayMin: number;
  delayMax: number;
}

interface IItem {
  key: number;
  type: string;
  delay: number;
  duration: number;
  top: number;
  left: number;
  startTop: number;
  startLeft: number;
  endTop: number;
  endLeft: number;
}

function getRandomInteger(low: number, high: number): number {
  return low + Math.floor(Math.random() * (high - low));
}

function createItem(config: IRedpacketRainConfig, type: string, key: number) {
  const startLeft = getRandomInteger(0, config.moveWidth + config.viewportWidth - config.starWidth);
  const endLeft = startLeft - config.moveWidth;
  const item: IItem = {
    key,
    type,
    delay: getRandomInteger(config.delayMin, config.delayMax),
    duration: getRandomInteger(config.durationMin, config.durationMax),
    startTop: -config.starHeight,
    top: -config.starHeight,
    startLeft,
    left: startLeft,
    endTop: config.viewportHeight,
    endLeft,
  };
  if (type === 'redpacket') {
    item.startTop = -config.redpacketHeight;
    (item.top = -config.redpacketHeight),
    (item.startLeft = getRandomInteger(0, config.viewportWidth + config.moveWidth - config.redpacketWidth));
    item.left = item.startLeft;
    item.endLeft = item.startLeft - config.moveWidth;
  }
  return item;
}

export function createItems(config: IRedpacketRainConfig, seconds: number) {
  const quantity = seconds * config.redpacketFate;
  const newItems: IItem[] = [];
  let itemKey = 0;
  let index = 0;
  for (let i = 0; i < quantity; i++) {
    const isStar = i % config.starFate;
    if (isStar) {
      newItems.push(createItem(config, `star${++index % 3}`, ++itemKey));
    } else {
      newItems.push(createItem(config, 'redpacket', ++itemKey));
    }
  }
  return newItems;
}

export default {
  getMoveWidth, getSeconds, trimString, checkReg, createItems,
};
